<?php

namespace app\seller\model;

use think\Db;
use think\Model;

class User extends Model
{

    // 表名
    protected $name = 'user';
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    // 追加属性
    protected $append = [
        'prevtime_text',
        'logintime_text',
        'jointime_text',
	    'level_text',
	    'gender_text',
	    'status_text',
	    'is_buy_cart_text',
    ];

    protected static function init()
    {
        self::beforeUpdate(function ($row) {
            $changed = $row->getChangedData();
            //如果有修改密码
            if (isset($changed['password']))
            {
                $salt = \fast\Random::alnum();
                $row->password = \app\common\library\Auth::instance()->getEncryptPassword($changed['password'], $salt);
                $row->salt = $salt;
            }
        });
    }
	
	
	const LEVEL_USER = 0;
	const LEVEL_CENTER = 1;
	const LEVEL_AGENT = 2;
	const LEVEL_COMPANY = 3;
	const LEVEL_OPERATIONS = 4;
	
	public static $_level = [
		self::LEVEL_USER => '普通会员',
		self::LEVEL_CENTER => '报单中心',
		self::LEVEL_AGENT => '代理商',
		self::LEVEL_COMPANY => '分公司',
		self::LEVEL_OPERATIONS => '运营中心'
	];
 
	public function getLevelList(){
		return self::$_level;
	}
	
	public function getLevelTextAttr($value, $data) {
		$value = $value ? $value : $data['level'];
		return isset(self::$_level[$value]) ? self::$_level[$value] : '';
	}
	
	const GENDER_MALE = 1;
	const GENDER_FEMALE = 0;

    public function getGenderList()
    {
        return [self::GENDER_MALE => '男', self::GENDER_FEMALE => '女'];
    }
	
	public function getGenderTextAttr($value, $data) {
		$value = $value ? $value : $data['gender'];
		$list = $this->getGenderList();
		return isset($list[$value]) ? $list[$value] : '';
	}
	
	
	const STATUS_Y = 1;
    const STATUS_N = 0;
    
    public function getStatusList()
    {
        return [self::STATUS_Y => '正常', self::STATUS_N => '禁用'];
    }
	
	public function getStatusTextAttr($value, $data) {
		$value = $value ? $value : $data['status'];
		$list = $this->getStatusList();
		return isset($list[$value]) ? $list[$value] : '';
	}
	
	
	const IS_BUY_CART_Y = 1;
	const IS_BUY_CART_N = 0;
	
	public function getIsBuyCartList()
	{
		return [self::IS_BUY_CART_Y => '是', self::IS_BUY_CART_N => '否'];
	}
	
	public function getIsBuyCartTextAttr($value, $data) {
		$value = $value ? $value : $data['is_buy_cart'];
		$list = $this->getIsBuyCartList();
		return isset($list[$value]) ? $list[$value] : '';
	}

    public function getPrevtimeTextAttr($value, $data)
    {
        $value = $value ? $value : $data['prevtime'];
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    public function getLogintimeTextAttr($value, $data)
    {
        $value = $value ? $value : $data['logintime'];
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    public function getJointimeTextAttr($value, $data)
    {
        $value = $value ? $value : $data['jointime'];
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setPrevtimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

    protected function setLogintimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

    protected function setJointimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

    public function group()
    {
        return $this->belongsTo('UserGroup', 'group_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    
    public function recommend(){
    	return $this->belongsTo('User', 'recommend_id', 'id', '', 'LEFT')->setEagerlyType(0);
    }
	
	
	/**
	 * 查看用户的信息，推荐人，实名认证，token
	 * @param $id
	 * @return array|false|\PDOStatement|string|Model
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\ModelNotFoundException
	 * @throws \think\exception\DbException
	 */
    public function getRelationInfo($id){
	    $userInfo = Db::name('user')->alias('u')
		    ->join('userVerified v', 'u.id = v.user_id', 'left')
		    ->join('user r', 'r.id = u.recommend_id', 'left')
		    ->join('userToken t', 'u.id = t.user_id', 'left')
		    ->field('u.id, u.username, u.nickname, u.mobile, u.level, u.integral, from_unixtime(u.jointime) as join_time, u.status, u.agent, u.balance, u.phone_balance, u.voucher_balance, u.phone_time, v.true_name, v.card_no, v.status as card_status, v.remark as verified_remark, r.mobile as recommend_mobile, r.username as recommend_username, t.token')
		    ->order(['t.createtime' => 'DESC'])
		    ->find($id);
	    
	    $userInfo['level_text'] = self::$_level[$userInfo['level']];
	    return $userInfo;
    }
}
